/**
 * Created by lyl on 15/4/11.
 */

jQuery.extend({
    // 设置 apDiv
    confirm:function (options) {
        var config = {
            title: null,
            message: 'hw-confirm',
            cancle: {
                isShow: false,
                isLeft: false,
                text: '取消'
            },
            ok: {
                isShow: true,
                text: '确定'
            },
            oncancle: function(){},
            onok: function(){}
        };
        config =$.extend( true,  {},config,options);
        var is_include_animate = testAnimate();
        //创建弹框和内容,显示内容
        var $confirm = $('<div class="hw-confirm"></div>');
        var $mask = $('<div class="hw-confirm-mask"></div>');
        if(!is_include_animate){
            $confirm.hide()//css('display','none');
        }
        function create_confirm(){
            var $msg_container = $('<div class="cf-msg-container"><div class="cf-msg"></div></div>');
            //title
            if(config.title){
                $msg_container.find('.cf-msg').append('<div class="cf-msg-title">' + config.title + '</div>');
            }
            //message
            $msg_container.find('.cf-msg').append('<div class="cf-msg-body">' + config.message + '</div>');
            $confirm.append($msg_container);
            //buttons
            var actions = {element: $('<div class="cf-action"></div>')};
            actions.ok = $('<div class="cf-action-item"><a href="javascript:void(0);" id="cf-btn-ok">' + config.ok.text + '</a></div>');
            actions.ok.click(function(){
                destory();
                config.onok();
            });
            if(config.cancle.isShow){
                actions.cancle = $('<div class="cf-action-item"><a href="javascript:void(0);" id="cf-btn-cancle">' + config.cancle.text + '</a></div>');
                actions.cancle.click(function(){
                    destory();
                    config.oncancle();
                });
            }
            if(config.cancle.isLeft){
                actions.element.append(actions.cancle);
                actions.element.append(actions.ok);
            }else{
                actions.element.append(actions.ok);
                actions.element.append(actions.cancle);
            }
            $confirm.append(actions.element);
            //if($('.hw-confirm').length > 0){
            //    $confirm.remove();
            //}
            $confirm.appendTo('body');
        }
        function createMask(){
            $mask.appendTo('body').fadeIn();
        }
        function open(){
            createMask();
            if(is_include_animate){
                $confirm.addClass('animated quick zoomIn');
            }else{
                $confirm.fadeIn(300);
            }
        }
        function destory(){
            if(is_include_animate){
                $confirm.addClass('zoomOut');
                setTimeout(function(){
                    $confirm.remove();
                },300);
            }else{
                $confirm.fadeOut(300,function(){
                    $confirm.remove();
                });
            }
            $mask.remove();
        }
        function testAnimate(){
            var es=document.getElementsByTagName('link');
            for(var i=0;i<es.length;i++){
                if(es[i]['href'].indexOf('animate')!=-1)return true;
            }
            return false;
        }
        function init(){
            create_confirm();
            open();
        }
        init();
    }
    , notification:function(options){
        var id = 'hw-notification';
        var config = {
            message: 'notification'
            , position: 'top' // position is value one of [top,bottom]
            , speed: 3 * 1000
        };
        var timer = null;
        config =$.extend( true,  {},config,options);
        var $body = $('<div></div>').attr('id', id);
        var $message = $('<div class="notification-body"></div>');
        //创建
        function create(){
            $body.append($message);//
            $body.appendTo('body');
        }
        function init(){
            if($('#'+id).length > 0){
                $('#' + id).remove();
            }
            create();
        }
        this.alert = function(msg){
            if(timer){
                $body.removeClass(config.position);
                clearTimeout(timer);
            }
            config.message = msg;
            $message.text(config.message);
            $body.addClass(config.position);
            timer = setTimeout(function(){
                $body.removeClass(config.position);
            },config.speed);
        };
        init();
        return this;
    }
    , loading: function(loadingTip){
        var loadingMsg = loadingTip || '';
        var $loadingBody = $('<div class="loading"><div class="loading-cell"><div class="loading-process"></div></div><div class="loading-cell"><div><span class="loading-tip">'+loadingMsg+'</span><span class="process-number"></span></div></div></div>').hide();
        var $mask = $('<div class="hw-confirm-mask"></div>');
        this.show = function(tip){
            var tmp = tip || loadingMsg;
            $loadingBody.find('.loading-tip').text(tmp);
            $loadingBody.appendTo('body').show();
            $mask.appendTo('body').fadeIn();
        };
        this.hide = function(){
            $loadingBody.remove();
            $mask.remove();
        };
        return this;
    }
});